<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
  request.setAttribute("now",new Date());
%>

<div class="row">
    <div class="col-lg-12">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title">任务发起登记表</h3>
            </div>

            <div style="margin: 10px">

                <!-- Nav tabs -->
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#bsc-tab-1" data-toggle="tab">任务督办单</a></li>
                    <c:if test="${task.numStatus==1}">
                        <li><a href="#bsc-tab-2" data-toggle="tab">任务反馈</a></li>
                        <li><a href="#bsc-tab-3" data-toggle="tab">历史副本</a></li>
                        <li><a href="#bsc-tab-4" data-toggle="tab">关联任务</a></li>
                        <li><a href="#bsc-tab-5" data-toggle="tab">相关附件</a></li>
                    </c:if>
                </ul>

                <!-- Tabs Content -->
                <div class="tab-content">

                    <div class="tab-pane pad-btm fade in active" id="bsc-tab-1">
                        <!-- BASIC FORM ELEMENTS -->
                        <!--===================================================-->
                        <form id="form_task_input" class="panel-body form-horizontal form-padding">
                            <input type="hidden" name="keyId" id="keyId" value="${task.id}" />
                            <input type="hidden" name="updateUnid" id="updateUnid" value="${task.updateUnid}" />

                            <div class="form-group">
                                <label class="col-md-2 control-label">所属项目<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <input type="text" id="dictItem" name="dictItem" class="form-control" value="${task.dictItem.name}" disabled>
                                </div>
                            </div>

                            <c:if test="${task.numStatus==0}">
                                <div class="form-group">
                                    <label class="col-md-2 control-label">
                                        关联任务
                                    </label>
                                    <div class="col-md-6 input-group mar-btm">
                                        <input type="text" id="relateTaskNames" name="relateTaskNames" class="form-control" disabled
                                               value="<c:forEach var="item" items="${task.relateTasks}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                                    </div>
                                </div>
                            </c:if>

                            <div class="form-group">
                                <label class="col-md-2 control-label">任务名称<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <input type="text" id="name" name="name" class="form-control" disabled value="${task.name}">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-2 control-label">详细要求<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <input type="hidden" id="detail" name="detail" class="form-control" value="${task.detail}">
                                    <div id="detail-summernote">

                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-2 control-label">
                                    任务责任人
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                </label>
                                <div class="col-md-6">
                                    <input type="text" id="leaderNames" name="leaderNames" class="form-control" disabled
                                           value="<c:forEach var="item" items="${task.leaders}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-2 control-label">
                                    任务组员
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                </label>
                                <div class="col-md-6">
                                    <input type="text" id="memberNames" name="memberNames" class="form-control" disabled
                                           value="<c:forEach var="item" items="${task.members}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                                </div>
                            </div>

                            <div class="form-group div-Hidden">
                                <label class="col-md-2 control-label">计划开始时间<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <div class="input-group date">
                                        <input type="text" class="form-control" id="datPlanStart" name="datPlanStart" disabled
                                               value=<fmt:formatDate pattern="yyyy-MM-dd" value="${task.datPlanStart}" /> >
                                        <span class="input-group-addon"><i class="ti-calendar"></i></span>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group div-Hidden">
                                <label class="col-md-2 control-label">计划完成时间<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <div class="input-group date">
                                        <input type="text" class="form-control" id="datPlanEnd" name="datPlanEnd" disabled
                                               value=<fmt:formatDate pattern="yyyy-MM-dd" value="${task.datPlanEnd}" /> >
                                        <span class="input-group-addon"><i class="ti-calendar"></i></span>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group div-Hidden">
                                <label class="col-md-2 control-label">预警时间<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <div class="input-group date">
                                        <input type="text" class="form-control" id="datWarning" name="datWarning" disabled
                                               value=<fmt:formatDate pattern="yyyy-MM-dd" value="${task.datWarning}" /> >
                                        <span class="input-group-addon"><i class="ti-calendar"></i></span>
                                    </div>
                                </div>
                            </div>


                            <div class="form-group">
                                <label class="col-md-2 control-label">优先程度<i class="fa fa-asterisk txt-color-red"></i>
                                </label>
                                <div class="col-md-6">
                                    <input type="text" id="priority" name="priority" class="form-control" disabled value="${priority}">
                                </div>
                            </div>

                            <c:if test="${task.numStatus==0}">
                                <div class="form-group">
                                    <label class="col-md-2 control-label">相关附件</label>
                                    <div class="col-md-6">
                                        <input name="uploadify" id="fileAttachment" placeholder="" type="file" >
                                        <input name="hiddenFiles" id="hiddenFilesAttachment" type="hidden" value="${task.fileAttachment}">
                                    </div>
                                </div>
                            </c:if>

                            <div class="form-group">
                                <label class="col-md-2 control-label">提交人</label>
                                <div class="col-md-2">
                                    <p class="form-control-static">${task.creater.name}</p>
                                </div>
                                <label class="col-md-2 control-label">提交日期</label>
                                <div class="col-md-2">
                                    <p class="form-control-static"><fmt:formatDate pattern="yyyy-MM-dd" value="${task.dateApply}" /></p>
                                </div>
                            </div>

                        </form>

                        <!--===================================================-->
                        <!-- END BASIC FORM ELEMENTS -->

                        <div class="panel-footer">
                            <a class="btn btn-default" id="apply-re-common" href="javascript:void(0)">
                                <i class="fa fa-lg fa-mail-reply"></i> 返回视图
                            </a>
                            <c:if test="${task.numStatus==0 && user.id==task.creater.id}">
                                <a class="btn btn-warning" key="ajax_task_edit" href="javascript:void(0);"><i class="fa fa-lg fa-edit"></i> 编辑表单 </a>
                            </c:if>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="bsc-tab-2">
                        <div class="table-responsive" style="min-height: 530px;margin: 10px">
                            <table id="task_feedback_table"></table>
                            <div id="task_feedback_page"></div>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="bsc-tab-3">
                        <div class="table-responsive" style="min-height: 530px;margin: 10px">
                            <table id="task_history_table"></table>
                            <div id="task_history_page"></div>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="bsc-tab-4">
                        <form id="form_task_relate" class="panel-body form-horizontal form-padding">
                            <div class="form-group">
                                <label class="col-md-2 control-label">
                                    <a href="javascript:void(0);" onclick="fn_tasks_dialog_select('relateTaskNamesR','relateTaskIdsR')" style="color: #0a6dbd">关联任务</a>
                                </label>
                                <div class="col-md-6">
                                    <input type="text" id="relateTaskNamesR" name="relateTaskNamesR" class="form-control" disabled
                                           value="<c:forEach var="item" items="${task.relateTasks}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                                    <%--<span class="input-group-btn">--%>
                                        <%--<button onclick="clearRelateTasks('relateTaskNamesR','relateTaskIdsR')" class="btn btn-mint" type="button"><i class="fa fa-close"></i></button>--%>
                                    <%--</span>--%>
                                    <input type="hidden" id="relateTaskIdsR" name="relateTaskIdsR"
                                           value="<c:forEach var="item" items="${task.relateTasks}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                                </div>
                                <div class="col-md-2">
                                    <a class="btn btn-warning" onclick="fn_task_save_relate()" href="javascript:void(0);"><i class="fa fa-upload"></i> 保存关联 </a>
                                </div>
                            </div>
                        </form>

                        <div class="table-responsive" style="min-height: 530px;margin: 10px">
                            <table id="task_relate_table"></table>
                            <div id="task_relate_page"></div>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="bsc-tab-5">
                        <form id="form_task_uploadfile" class="panel-body form-horizontal form-padding">
                            <div class="form-group">
                                <label class="col-md-2 control-label">
                                    相关附件
                                </label>
                                <div class="col-md-6">
                                    <input name="uploadify" id="fileAttachmentR" placeholder="" type="file" >
                                    <input name="hiddenFilesR" id="hiddenFilesAttachmentR" type="hidden" value="${task.fileAttachment}">
                                </div>
                                <div class="col-md-2">
                                    <a class="btn btn-warning" onclick="fn_task_upload_file()" href="javascript:void(0);"><i class="fa fa-upload"></i> 保存相关附件 </a>
                                </div>
                            </div>
                        </form>
                    </div>

                </div>
            </div>

        </div>
    </div>
</div>

<script>

    // 附件显示
    readLoad({
        objId:"hiddenFilesAttachment",
        entityName:"fileAttachment",
        sourceId:"hiddenFilesAttachment"
    });

    // 附件上传
    inputLoad({
        objId:"fileAttachmentR",
        entityName:"fileAttachmentR",
        sourceId:"hiddenFilesAttachmentR"
    });

    function fn_tasks_dialog_select(nameField,idField) {
        __open_dialog_select('选择关联任务', '<%=path%>/activiti/task/dialogs?sids='+$("#"+idField).val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 获取 user
                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_tasks_select(callback);
        });
    }

    function clearRelateTasks(nameField,idField) {
        $("#"+nameField).val("");
        $("#"+idField).val("");
        __toastr_info("选择的关联任务已被清空！");
    }

    //返回视图
    $("#apply-re-common").click(returnToHashUrl);

    //编辑
    $("a[key='ajax_task_edit']").unbind("click").bind("click",function(){
        loadURL("<%=path%>/activiti/task/edit/"+$("#keyId").val()+"/n",$('#page-content'));
    });

    // 加载完成后执行
    $(function(){
        //赋值
        var sHtml = $("#detail").val();
        if(sHtml.length>0){
            $('#detail-summernote').summernote("code",sHtml);
            $('.note-editable').attr('contenteditable', false);
            $('.note-control-selection').remove();
        }

        init_grid_task_feedback();

        init_grid_task_history();

        init_grid_task_relate();
    })

    function init_grid_task_feedback() {
        __init_jqgrid('task_feedback_table', 'task_feedback_page',
            '<%=path%>/activiti/taskfeedback/list?taskId='+$("#form_task_input #keyId").val(),
            ['反馈人', '反馈日期', '完成进度', '操作', 'id'],
            [
                {name: 'creater', index: 'creater', width: 50, sortable: false, search: false},
                {name: 'createDate', index: 'createDate', width: 50, sortable: false, search: false},
                {name: 'feedbackContent', index: 'feedbackContent', width: 200, sortable: false, stype: 'text', searchoptions: {sopt: ['cn']}},
                {name: 'act', index: 'act', width: 150, fixed: true, sortable: false, search: false, formatter: actFormatFeedback},
                {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,
            {
                autowidth: false,
                width: 1000,
                height: 500
            }
        );
    }

    function init_grid_task_history() {
        __init_jqgrid('task_history_table', 'task_history_page',
            '<%=path%>/activiti/task/list?updateUnid='+$("#updateUnid").val(),
            ['所属项目', '任务名称', '任务负责人', '计划开始时间', '计划完成时间','优先级','提交日期', '操作', 'id'],
            [
                {name: 'dictItemName', index: 'dictItemName', width: 60, sortable: true, search: false},
                {name: 'name', index: 'name', width: 120, sortable: false, stype: 'text', searchoptions: {sopt: ['cn']}},
                {name: 'leaders', index: 'leaders', width: 60, sortable: true, search: false},
                {name: 'datPlanStart', index: 'datPlanStart', width: 60, sortable: false, search: false},
                {name: 'datPlanEnd', index: 'datPlanEnd', width: 60, sortable: false, search: false},
                {name: 'priority', index: 'priority', width: 40, sortable: false, search: false},
                {name: 'dateApply', index: 'dateApply', width: 50, sortable: false, search: false},
                {name: 'act', index: 'act', width: 150, fixed: true, sortable: false, search: false, formatter: actFormat},
                {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,
            {
                autowidth: false,
                width: 1400,
                height: 500,
                postData: {catalog: 0x01}
            }
        )
    }

    function init_grid_task_relate() {
        __init_jqgrid('task_relate_table', 'task_relate_page',
            '<%=path%>/activiti/task/listR?taskId='+$("#form_task_input #keyId").val(),
            ['所属项目', '任务名称', '任务负责人', '计划开始时间', '计划完成时间','优先级','提交日期', '操作', 'id'],
            [
                {name: 'dictItemName', index: 'dictItemName', width: 60, sortable: true, search: false},
                {name: 'name', index: 'name', width: 120, sortable: false, stype: 'text', searchoptions: {sopt: ['cn']}},
                {name: 'leaders', index: 'leaders', width: 60, sortable: true, search: false},
                {name: 'datPlanStart', index: 'datPlanStart', width: 60, sortable: false, search: false},
                {name: 'datPlanEnd', index: 'datPlanEnd', width: 60, sortable: false, search: false},
                {name: 'priority', index: 'priority', width: 40, sortable: false, search: false},
                {name: 'dateApply', index: 'dateApply', width: 50, sortable: false, search: false},
                {name: 'act', index: 'act', width: 150, fixed: true, sortable: false, search: false, formatter: actFormat},
                {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,
            {
                autowidth: false,
                width: 1400,
                height: 500,
                postData: {catalog: 0x01}
            }
        )
    }

    function actFormatFeedback(cellvalue, options, rowObject){
        var id = rowObject.id;
        cellvalue = '<button class="btn btn-white btn-sm" data-original-title="查看" onclick="fn_taskfeedback_read(\'' + id + '\');"><i class="fa fa-eye"> </i> 查看</button> ';
        return cellvalue;
    }

    function fn_taskfeedback_read(id) {
        var url = '<%=path%>/activiti/taskfeedback/read/'+id;
        __open_dialog_form_read('任务反馈表', url);
    }

    function actFormat(cellvalue, options, rowObject){
        var id = rowObject.id;
        cellvalue = '<button class="btn btn-white btn-sm" data-original-title="查看" onclick="fn_task_read(\'' + id + '\');"><i class="fa fa-eye"> </i> 查看</button> ';
        return cellvalue;
    }

    function fn_task_read(id) {
        var url = '<%=path%>/activiti/task/readhistory/'+id;
        __open_dialog_form_read('任务发起登记表', url);
    }

    function fn_task_upload_file() {
        $.ajax({
            url: '<%=path%>/activiti/task/uploadfile',
            type: "POST",
            cache: false,
            dataType: 'json',
            data: {
                keyId: $("#form_task_input #keyId").val(),
                hiddenFiles: $("input[name='hiddenFilesR']").eq(0).val(),
                fileAttachment: $("input[name='fileAttachmentR']").eq(0).val()
            },
            success: function(data) {
                __toastr(data);
            },
            error:function() {
                __toastr_error('上传数据出错');
            }
        });
    }

    function fn_task_save_relate() {
        $.ajax({
            url: '<%=path%>/activiti/task/saverelate',
            type: "POST",
            cache: false,
            dataType: 'json',
            data: {
                keyId: $("#form_task_input #keyId").val(),
                relateTaskIds: $("#relateTaskIdsR").val()
            },
            success: function(data) {
                __toastr(data);
            },
            error:function() {
                __toastr_error('关联任务出错');
            }
        });
    }
</script>
